package springMvc;

import com.hqj.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
@RequestMapping("/fc")
public class FileController extends BaseController {

    @RequestMapping("/fileUpload")
    public String fileUpload(){
        return prefix + "/fileUpload.jsp";
    }

    @RequestMapping("/uploadFile")
    public String uploadFile(HttpServletRequest req,
            @RequestParam(required = false,value = "fileResource")MultipartFile files[]){
        boolean b = saveFile(files);
        String result = b?"成功":"失败";
        req.setAttribute("result",result);
        return prefix + "/result.jsp";
    }


    @RequestMapping("/fileDownload")
    public String fileDownload(){
        return prefix + "/fileDownload.jsp";
    }

    @RequestMapping("/downloadFile")
    public void downloadFile(HttpServletRequest req, HttpServletResponse res){
        String fileName = req.getParameter("fileName");

        String downFileName = "D:/files/系主任.docx";
        File f1 = new File(downFileName);
        if(!f1.exists()){
            System.out.println("文件不存在，无法下载!");
            return;
        }

        //设置输出的编码格式
        res.setCharacterEncoding("utf-8");
        //设置下载后的文件名称
        res.setHeader("Content-Disposition","attachment;fileName="+fileName+".docx");
        //设置下载非打开
        res.setContentType("multipart/form-data");
        try {

            //字节输入流，输入文件内容
            InputStream is = new FileInputStream(f1);
            //字节输出流，输出文件内容
            OutputStream os = res.getOutputStream();
            byte bs[] = new byte[1024];
            int i = 0;
            while((i=is.read(bs))>0){
                os.write(bs,0,i);
            }
            os.close();
            is.close();

        }catch (Exception e){
            e.printStackTrace();
        }


    }

    /**
     * 保存文件
     * @return
     */
    public boolean saveFile(MultipartFile files[]){
        try {
            if (files.length > 0) {
                String path = "D:/files";
                File f1 = new File(path);
                FileUtil.createDir(f1);
                //遍历文件
                for (MultipartFile file : files) {
                    //获取文件名
                    String fileName = file.getOriginalFilename();
                    //中文乱码处理
                    fileName = new String(fileName.getBytes("iso-8859-1"),"utf-8");
                    File f2 = new File(f1, fileName);
                    FileUtil.createFile(f2);
                    //文件的复制粘贴
                    file.transferTo(f2);
                }
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

}

